package com.mall.b2c.plugin.search;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.mall.b2c.domain.goods.GoodsCategoryDomain;
import com.mall.common.hibernate.Finder;
import com.mall.common.plugin.AutoRegisterPlugin;
import com.mall.common.util.UrlUtils;

/**
 * 商品剂型筛选器
 * 
 * @author wangzx
 * 
 */
public class GoodsTypeSearchFilter { //extends AutoRegisterPlugin implements IGoodsSearchFilter {

/*	//@Override
	public void filter(Finder finder, GoodsCategoryDomain cat,
			String urlFragment) {
		if (StringUtils.isNotBlank(urlFragment)) {
			if (urlFragment.equals(GoodsTypeHelper.ZY) || urlFragment.equals(GoodsTypeHelper.ZYYP)) {
				finder.append(" and g.goodsType in (:goodsType)");
				if (urlFragment.equals(GoodsTypeHelper.ZY)) {
					finder.setParamList("goodsType", new String[] {"103006", "103019"});
				} else {
					finder.setParamList("goodsType", new String[] {"103007", "103020"});
				}
			}
		}
	}

	//@Override
	public List<SearchSelector> createSelectorList(GoodsCategoryDomain cat,
			String url, String urlFragment, Map<String, String> paramMap,
			String hql, Finder finder) {
		List<SearchSelector> searchSelectors = new ArrayList<SearchSelector>();
		*//** 已选择商品分类 *//*
		if (StringUtils.isNotBlank(urlFragment)) {
			if (urlFragment.equals(GoodsTypeHelper.ZY) || urlFragment.equals(GoodsTypeHelper.ZYYP)) {
				SearchSelector searchSelector = new SearchSelector();
				searchSelector.setName(urlFragment.equals(GoodsTypeHelper.ZY) ? "中药" : "中药饮片");
				searchSelector.setSelected(Boolean.TRUE);
				searchSelector.setUrl(url);
				searchSelectors.add(searchSelector);
				return searchSelectors;
			}
		}
		finder.resetHQL("select distinct g.goodsType from " + hql);
		List<String> goodsTypeList = finder.findHQLList();

		if (!CollectionUtils.isEmpty(goodsTypeList)) {
			boolean hasZY = false;
			boolean hasZYYP = false;
			SearchSelector searchSelector = null;
			for (String goodsType : goodsTypeList) {
				if(StringUtils.isBlank(goodsType)) {
					continue;
				}
				if((goodsType.equals("103006") || goodsType.equals("103019")) && !hasZY) {
					hasZY = true;
					searchSelector = new SearchSelector();
					searchSelector.setName("中药材");
					searchSelector.setUrl(UrlUtils.addUrlParam(url, getFilterId(), GoodsTypeHelper.ZY, false));
					searchSelector.setSelected(Boolean.FALSE);
					searchSelectors.add(searchSelector);
				} else if((goodsType.equals("103007") || goodsType.equals("103020")) && !hasZYYP) {
					hasZYYP = true;
					searchSelector = new SearchSelector();
					searchSelector.setName("中药饮片");
					searchSelector.setUrl(UrlUtils.addUrlParam(url, getFilterId(), GoodsTypeHelper.ZYYP, false));
					searchSelector.setSelected(Boolean.FALSE);
					searchSelectors.add(searchSelector);
				}
			}
		}
		return searchSelectors;
	}

	@Override
	public String getFilterId() {
		return "goodsType";
	}

	@Override
	public String getSysId() {
		return "goodsTypeList";
	}

	@Override
	public String getType() {
		return "goodsSearchFilter";
	}

	@Override
	public String getId() {
		return "goodsTypeSearchFilter";
	}

	@Override
	public String getName() {
		return "商品类型筛选器";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "wangzx";
	}

	@Override
	public void perform(Object... params) {

	}

	@Override
	public void register() {

	}*/
}
